package com.lhn.netty.nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Channel demo
 *
 * @author lhn
 * @date 2025/05/26 10:09
 * @since 1.0.0
 **/
public class NIOFileChannel01 {

    public static void main(String[] args) throws Exception {
        String str = "hello world";
        // 创建一个输入流 -> channel
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\test\\file01.txt");
        // 通过 fileoutputstram 获取 fileChannel
        // fileChannel 真实类型是  FileChannelImpl
        FileChannel fileChannel = fileOutputStream.getChannel();
        // 查看其对应的真是类
        System.out.println(fileChannel.getClass());
        // 创建一个缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        // 将str放入缓冲区
        byteBuffer.put(str.getBytes());
        // 将缓冲区数据写入channel
        byteBuffer.flip();
        fileChannel.write(byteBuffer);
        fileOutputStream.close();
    }

}